<template>
  <div class="container">
    <div class="top">
      <el-form label-width="100px" class="demo-ruleForm">
        <el-form-item label="选择库存：">
          <el-select
            v-model="select.is_locks"
            @change="selectData()"
            placeholder="请选择"
          >
            <el-option
              v-for="item in select.options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-input
            style="width: 300px"
            :disabled="true"
            placeholder="请搜索商品信息"
            v-model="commodityNameList.name"
          >
            <template slot="prepend">商品</template>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="commodityBtn"
            ></el-button>
          </el-input>
          <el-input
            style="width: 300px"
            :disabled="true"
            placeholder="请搜索仓库"
            v-model="warehouseList.name"
          >
            <template slot="prepend">仓库</template>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="warehouseBtn"
            ></el-button>
          </el-input>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-search"
            @click="handleSearch()"
            >查询</el-button
          >
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="empty()"
            >清空</el-button
          >
        </el-form-item>
        <el-button
          v-if="select.is_locks == 1"
          class="add"
          icon="el-icon-circle-plus"
          size="small"
          type="primary"
          @click="addPermission"
          >盘点</el-button
        >
      </el-form>
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="select.is_locks == 1"
        type="selection"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stock"
        label="库存"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_name"
        label="仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_code"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="spec"
        label="规格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="freeze_num"
        label="冻结"
        align="center"
      ></el-table-column>
      <el-table-column prop="qty" label="件数" align="center"></el-table-column>
      <!-- <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button type="info" size="mini" icon="el-icon-info" @click="particulars(scope)">详情</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- dialog -->
    <checkDialog
      :dialog="dialog"
      @upData="getData"
      :formId="formId"
    ></checkDialog>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
  </div>
</template>
<script>
import CheckDialog from "@/components/check/checkDialog";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import Pagination from "@/components/pagination";
import { checkList } from "@/api/check/check";
import Warehouseselect from "@/components/commodity/warehouseselect";
export default {
  components: {
    pagination: Pagination,
    checkDialog: CheckDialog,
    commodityDialog: CommodityDialog,
    warehouseselect: Warehouseselect,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      warehouseselect: {
        centerDialogVisible: false,
      },
      warehouseList: {
        name: "",
        code: "",
      },
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
      },
      commoditydialog: {
        centerDialogVisible: false,
      },
      commodityNameList: {
        name: "",
        code: "",
      },
      formData: [],
      formId: [],
      select: {
        options: [
          {
            value: 1,
            label: "锁定商品",
          },
          {
            value: 0,
            label: "所有商品",
          },
        ],
        is_locks: 1,
      },
      searchName: "",
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        is_lock: this.select.is_locks,
        goods_id: this.commodityNameList.code,
        warehouse_code: this.warehouseList.code,
      };
      checkList(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: "",
      };
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true,
      };
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true,
      };
    },
    //盘点
    addPermission() {
      this.formId = [];
      if (this.formData.length == 0) {
        this.$message({
          message: "请选择商品",
          type: "info",
        });
      } else {
        this.dialog.centerDialogVisible = true;
        this.formData.forEach((res) => {
          this.formId.push({
            goods_id: res.goods_id,
            spec: res.spec,
            warehouse_code: res.warehouse_code,
          });
        });
      }
    },
    //详情
    particulars(scope) {
      this.particularsDialogs.centerDialogVisible = true;
      this.particularsDialogs.id = scope.row.id;
    },
    // 搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //清空
    empty() {
      // this.select = {
      //   options: [
      //     {
      //       value: "0",
      //       label: "所有商品"
      //     },
      //     {
      //       value: "1",
      //       label: "锁定商品"
      //     }
      //   ],
      //   is_locks: 1,
      //   label: "锁定商品"
      // };
      this.commodityNameList = {
        name: "",
        code: "",
      };
      this.warehouseList.code = "";
      this.warehouseList.name = "";
    },
    selectData() {
      this.handleSearch();
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },

    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.formData = val;
      console.log(this.formData);
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  display: flex;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>